package cn.lmxdev.ptms.bean.vo;

import cn.lmxdev.ptms.enumeration.IBizHttpState;
import cn.lmxdev.ptms.enumeration.impl.BizHttpStateImpl;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.Collection;
import java.util.Map;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class ResponseVo<T> {
    private Integer code = null;
    private String msg = null;
    private Long count = null;
    private T data = null;

    @JsonIgnore
    public ResponseVo(IBizHttpState bizHttpState) {
        this.code = bizHttpState.getCode();
        this.msg = bizHttpState.getMsg();
    }

    @JsonIgnore
    public ResponseVo(T data) {
        if (data instanceof Boolean)
            if (!(Boolean) data) {
                this.code = BizHttpStateImpl.NO.getCode();
                this.msg = "操作失败";
            } else {
                this.code = BizHttpStateImpl.OK.getCode();
                this.msg = "操作成功";
            }
        else {
            this.code = BizHttpStateImpl.OK.getCode();
            this.count = data instanceof Collection ? ((Collection<?>) data).size() : (data instanceof Map ? ((Map<?, ?>) data).size() : 0L);
            this.data = data;
        }
    }

    @JsonIgnore
    public ResponseVo(Long count, T data) {
        this.code = BizHttpStateImpl.OK.getCode();
        this.count = count;
        this.data = data;
    }

    @JsonIgnore
    public ResponseVo(IBizHttpState bizHttpState, String msg) {
        this.code = bizHttpState.getCode();
        this.msg = msg;
    }
}